package ch.fusun.baron.farm;

import java.util.HashMap;
import java.util.Map;

import ch.fusun.baron.data.AbstractDataProvider;
import ch.fusun.baron.data.DataUpdate;
import ch.fusun.baron.map.Tile;

/**
 * The implementation of the farm service
 */
public class FarmServiceImpl extends AbstractDataProvider implements
		FarmService {

	private final Map<Tile, Farm> farmMap = new HashMap<Tile, Farm>();
	private int idCounter = 0;

	@Override
	public DataUpdate createFullUpdate() {
		return new FarmDataUpdate(farmMap);
	}

	@Override
	public void setFarm(Tile tile, Farm city) {
		farmMap.put(tile, city);
		updateAllListeners(new NewFarmDataUpdate(tile, city));
	}

	@Override
	public Farm getFarm(Tile model) {
		return farmMap.get(model);
	}

	@Override
	public Tile getLocation(Farm farm) {
		for (Tile tile : farmMap.keySet()) {
			if (farmMap.get(tile).equals(farm)) {
				return tile;
			}
		}
		return null;
	}

	@Override
	public void createFarm(Tile tile) {
		setFarm(tile, new Farm(idCounter++));
	}

}
